package com.example.administrator.dc.dialog;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.widget.Toast;

import com.example.administrator.dc.Bean.EditGender;
import com.example.administrator.dc.model.UsersModel;
import com.example.administrator.dc.util.SexListener;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by Administrator on 2017/9/1.
 */

public class UpdateDialog {

       int yourChoice ;


    public void select(final  Context context,final int gender,final SexListener listener,final  String userid) {
        final String[] items = {"男", "女"};
         yourChoice = -1;
        final AlertDialog.Builder singleChoiceDialog =
                new AlertDialog.Builder(context);
       //singleChoiceDialog.setTitle("选取照片方式");
        // 第二个参数是默认选项，此处设置为0
        singleChoiceDialog.setSingleChoiceItems(items, gender,
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(final DialogInterface dialog, final int which) {
                        yourChoice=which;
                        String s = String.valueOf(yourChoice);
                        Toast.makeText(context,yourChoice+"",Toast.LENGTH_SHORT).show();
                        if (yourChoice!=-1)
                        {
                            UsersModel usersModel =new UsersModel();
                            Call <EditGender> call= usersModel.editgender(userid, s);
                            call.enqueue(new Callback<EditGender>() {
                                @Override
                                public void onResponse(Call<EditGender> call, Response<EditGender> response) {
                                    if (1==response.body().getStatus())
                                    {
                                        listener.onSuccess(items[yourChoice],yourChoice);
                                        Toast.makeText(context,"修改成功！",Toast.LENGTH_SHORT).show();
                                    }

                                    dialog.dismiss();
                                }

                                @Override
                                public void onFailure(Call<EditGender> call, Throwable t) {
                                    Toast.makeText(context,"" +
                                            "网络错误！",Toast.LENGTH_SHORT).show();
                                }
                            });

                        }

                    }
                });
        singleChoiceDialog.show();

    }


}
